import 'package:go_router/go_router.dart';
import 'package:shouyin_app/common/base/base.dart';
import 'package:shouyin_app/pages/choose_table/choose_table.dart';
import 'package:shouyin_app/pages/demo/demo.dart';
import 'package:shouyin_app/pages/home/home.dart';
import 'package:shouyin_app/pages/login/login.dart';
import 'package:shouyin_app/pages/put_order/view.dart';
import 'package:shouyin_app/pages/splash/splash.dart';
import 'router.dart';

class AppRoutes {

  static final RouteObserver<Route> observer = RouteObservers();

  static List<String> history = [];

  static final GoRouter config = GoRouter(
    initialLocation: '/',
    observers: [observer],
    redirect: _RouteRedirect.auth,
    routes: [
      GoRoute(
        path: '/',
        name: AppPages.splash,
        redirect: (context, state) => null,
        pageBuilder: (context, state) => MaterialPage(
          name: state.uri.toString(),
          key: state.pageKey,
          child: const SplashView(),
        ),
      ),
      GoRoute(
        path: '/${AppPages.home}',
        name: AppPages.home,
        redirect: (context, state) => null,
        pageBuilder: (context, state) => MaterialPage(
          name: state.uri.toString(),
          key: state.pageKey,
          child: const HomeView(),
        ),
      ),
      GoRoute(
        path: '/${AppPages.login}',
        name: AppPages.login,
        pageBuilder: (context, state) => MaterialPage(
          name: state.uri.toString(),
          key: state.pageKey,
          child: const LoginView(),
        ),
      ),
      GoRoute(
        path: '/${AppPages.putOrder}',
        name: AppPages.putOrder,
        pageBuilder: (context, state) => CustomTransitionPage(
            name: state.uri.toString(),
            key: state.pageKey,
            child: const PutOrderView(),
            opaque: false,
            transitionsBuilder: (_, __, ___, child) => child
        ),
      ),
      GoRoute(
        path: '/${AppPages.chooseTable}',
        name: AppPages.chooseTable,
        pageBuilder: (context, state) => CustomTransitionPage(
            name: state.uri.toString(),
            key: state.pageKey,
            child: const ChooseTableView(),
            opaque: false,
            transitionsBuilder: (_, __, ___, child) => child
        ),
      ),
      GoRoute(
        path: '/${AppPages.demo}',
        name: AppPages.demo,
        pageBuilder: (context, state) => CustomTransitionPage(
            name: state.uri.toString(),
            key: state.pageKey,
            child: const DemoView(),
            opaque: false,
            transitionsBuilder: (_, __, ___, child) => child
        ),
      ),
    ],
  );
}

abstract class _RouteRedirect {
  static String? auth(BuildContext context, GoRouterState state) {
    // if (UserStore.to.isLogin) return null;
    // return '/${Routes.login}';
    return null;
  }
}